package com.market.domain;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

@Entity
@Table(name = "picture")
public class Picture extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3751104611340891621L;
	
	
	public enum TYPE{
		INDIVIDUAL_PRODUCT,MULTI_PRODUCT,ADVERTISEMENT,NEWS,FARM,STORE,EMPLOYEE,OTHER;
		
		private String getMessage(String key) {
			Locale locale = FacesContext.getCurrentInstance().getViewRoot()
					.getLocale();
			java.util.ResourceBundle resBundle = ResourceBundle.getBundle(
					"com.market.messages", locale);
			return resBundle.getString(key);
		}

		public String getLabel() {
			return this.getMessage("PICTURE_TYPE_" + this.toString());
		}
	}
	
	public String getFullFileName() {
		if (this.path == null || this.path.trim().equals("")) {
			return baseDir + "/" + this.fileName;
		} else {
			return baseDir + "/" + this.path + "/" + this.fileName;
		}
	}
	
	public StreamedContent getDataStream() {
		if (this.uploadedFile != null) {
			try {
				this.dataStream = new DefaultStreamedContent(
						this.uploadedFile.getInputstream(), "image/jpeg");
				return dataStream;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}
	
	/**
	 * The method generate path for saving picture, according to this picture's id;
	 * 1-1000 save to D001,1001-2000 save to D002, each directory save 1000 pictures
	 * @return
	 */
	
	public String generatePath() {
		String path;
		path = String.valueOf((int) Math.ceil(id / 1000d));
		if (path.length() == 1) {
			path = "00" + path;
		} else {
			if (path.length() == 2) {
				path = "0" + path;
			}
		}
		path = "D" + path;
		return path;

	}

	@Override
	public boolean equalsId(Object o) {
		if ((o instanceof Picture) && (((Picture) o).getId().equals(id))) {
			return true;
		}
		return false;
	}
	
	@Transient
	public Boolean getIsTitleEmpty(){
		if(this.getTitleLabel() ==null || this.getTitleLabel().isEmpty()){
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	@Transient
	public Boolean getIsDescriptionEmpty(){
		if(this.getDescriptionLabel() == null || this.getDescriptionLabel().isEmpty()){
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	public String getTitleLabel() {
		return super.getLabel("Title", super.getLocale());
	}
	
	
	public String getTitleLabel(Locale locale){
		return super.getLabel("Title", locale);
	}

	public String getDescriptionLabel() {
		return super.getLabel("Description", super.getLocale());
	}
	
	public String getDescriptionLabel(Locale locale) {
		return super.getLabel("Description", locale);
	}
	

	// This is the base directory for all pictures
	@Transient
	private static final String baseDir = "uploadedFiles/pictures";
	// This variable use only during upload picture;
	@Transient
	private UploadedFile uploadedFile;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@Column(length = 150)
	private String title;
	@Column(length = 150)
	private String chineseTitle;
	@Column(length = 150)
	private String frenchTitle;
	@Column(length = 150)
	private String path;
	@Column(length = 50)
	private String fileName;
	@Enumerated(EnumType.STRING)
	private TYPE type;
	@Column(nullable = false, columnDefinition = "bit(1) DEFAULT 0")
	private Boolean isHomePage = Boolean.FALSE;	
	@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	@Temporal(TemporalType.TIMESTAMP)
	private Date uploadedDate;
	@Column
	@Temporal(TemporalType.DATE)
	private Date takenDate;
	@Column(length = 500)
	private String description;
	@Column(length = 500)
	private String chineseDescription;
	@Column(length = 500)
	private String frenchDescription;	
	@Column(columnDefinition = "int Default 50")
	private int recommendIndex = 50;
	@Column(columnDefinition = "bit(1) default 0")
	private boolean isRecycled = false;

	@Transient
	private DefaultStreamedContent dataStream;

	


	public static String getBaseDir() {
		return baseDir;
	}

	public UploadedFile getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(UploadedFile uploadeFile) {
		this.uploadedFile = uploadeFile;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	

	public String getChineseTitle() {
		return chineseTitle;
	}

	public void setChineseTitle(String chineseTitle) {
		this.chineseTitle = chineseTitle;
	}

	public String getFrenchTitle() {
		return frenchTitle;
	}

	public void setFrenchTitle(String frenchTitle) {
		this.frenchTitle = frenchTitle;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public TYPE getType() {
		return type;
	}

	public void setType(TYPE type) {
		this.type = type;
	}
	
	public Boolean getIsHomePage() {
		return isHomePage;
	}

	public void setIsHomePage(Boolean isHomePage) {
		this.isHomePage = isHomePage;
	}

	public Date getUploadedDate() {
		return uploadedDate;
	}

	public void setUploadedDate(Date uploadedDate) {
		this.uploadedDate = uploadedDate;
	}

	public Date getTakenDate() {
		return takenDate;
	}

	public void setTakenDate(Date takenDate) {
		this.takenDate = takenDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	
	
	public String getChineseDescription() {
		return chineseDescription;
	}

	public void setChineseDescription(String chineseDescription) {
		this.chineseDescription = chineseDescription;
	}

	public String getFrenchDescription() {
		return frenchDescription;
	}

	public void setFrenchDescription(String frenchDescription) {
		this.frenchDescription = frenchDescription;
	}

	public int getRecommendIndex() {
		return recommendIndex;
	}

	public void setRecommendIndex(int recommendIndex) {
		this.recommendIndex = recommendIndex;
	}

	public boolean getIsRecycled() {
		return isRecycled;
	}

	public void setIsRecycled(boolean isRecycled) {
		this.isRecycled = isRecycled;
	}

	

	@Override
	public String toString() {
		return "Picture [id=" + id + ", title=" + title + ", path=" + path
				+ ", fileName=" + fileName + ", category=" + type
				+ ", uploadedDate=" + uploadedDate + ", takenDate=" + takenDate
				+ ", description=" + description + ", recommendIndex="
				+ recommendIndex + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((type == null) ? 0 : type.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((fileName == null) ? 0 : fileName.hashCode());
		result = prime * result + ((path == null) ? 0 : path.hashCode());
		result = prime * result + recommendIndex;
		result = prime * result
				+ ((takenDate == null) ? 0 : takenDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result
				+ ((uploadedDate == null) ? 0 : uploadedDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Picture other = (Picture) obj;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (fileName == null) {
			if (other.fileName != null)
				return false;
		} else if (!fileName.equals(other.fileName))
			return false;
		if (path == null) {
			if (other.path != null)
				return false;
		} else if (!path.equals(other.path))
			return false;
		if (recommendIndex != other.recommendIndex)
			return false;
		if (takenDate == null) {
			if (other.takenDate != null)
				return false;
		} else if (!takenDate.equals(other.takenDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (uploadedDate == null) {
			if (other.uploadedDate != null)
				return false;
		} else if (!uploadedDate.equals(other.uploadedDate))
			return false;
		return true;
	}

}
